<template>
  <div class="container">
    <div class="app-container">
      <!-- 组织架构 -->
      <!-- 只写 default-expand-all 相当于 :default-expand-all = ture  展示所有节点-->
      <!-- :expand-on-click-node="false" 点击树不折叠,点击图标才折叠-->
      <el-tree
        :expand-on-click-node="false"
        default-expand-all
        :data="depts"
        :props="defaultProps"
      >
        <!-- v-slot 只能在 template 加, v-slot="{ node, data }" -->
        <template v-slot="{data}">
          <!-- 节点结构 -->
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- 下拉菜单监听 command 事件 -->
              <!-- operateDept不写参数, 默认参数是 $event, 也就是下拉选项的command属性值,点击菜单传出来的类型 -->
              <el-dropdown @command="operateDept($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <!-- 下拉菜单选项添加 command 属性 -->
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 点击添加的弹层组件  -->
    <!-- 加.sync修饰 会监听子组件 update:showDialog 事件-->
    <!-- :current-node-id="currentNodeId"给子组件传数据 -->
    <!-- @updateDepartment="getDepartment"  监听updateDepartment事件, 调用获取部门函数 -->
    <!--  -->
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :current-node-id="currentNodeId" @updateDepartment="getDepartment" />
  </div>
</template>
<script>
import { delDepartment, getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import addDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      // 数据属性
      depts: [
        // {
        //   name: '传智教育',
        //   managerName: '管理员',
        //   children: [
        //     { name: '总裁办', managerName: '张三' },
        //     { name: '行政部', managerName: '李四' },
        //     { name: '财务部', managerName: '王五' }
        //   ]
        // }
      ],
      defaultProps: {
        label: 'name', // 要显示的字段的名字
        children: 'children' // 读取子节点的名字
      },
      showDialog: false,
      currentNodeId: null
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取部门列表
    async getDepartment() {
      const res = await getDepartment()
      this.depts = transListToTreeData(res, 0)
      // console.log(this.depts)
    },
    // 监听下拉菜单事件
    operateDept(type, id) {
      if (type === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.showDialog = true
        // 更新 props -- 异步
        this.currentNodeId = id
        // 如果直接写 this.$refs.addDept.getDepartmentDetail(), 是同步, 此时还获取不到 id
        // this.$nextTick() 里面写回调函数,表示完成渲染之后才进行调用
        this.$nextTick(() => {
          // 给子组件的标签绑定了ref属性, 那么 this.$refs.addDept.就相当于是 子组件中的 this, 可以调用子组件里面的方法
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartment(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped>
  .app-container {
    padding: 30px 140px;
    font-size: 14px;
  }
  .tree-manager{
    width: 50px;
    display: inline-block;
    margin:30px
  }
</style>
